package leilei.jace.com.tcpdemo3.test;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.lianyi.push.entity.TcpMsgBean;
import com.lianyi.push.share.NettyConfig;
import com.lianyi.push.share.listener.TcpClientToSeiviceMessageListener;
import com.lianyi.push.share.listener.UdpClientToSeiviceMessageListener;
import com.lianyi.push.share.tcpservice.TcpService;
import com.lianyi.push.share.udpservice.UdpService;
import com.lianyi.push.util.CommonUtils;
import com.lianyi.push.util.LogUtil;
import com.lianyi.push.util.ToastUtil;

import java.net.InetSocketAddress;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;

/**
 * Created by leilei on 2018/3/28.
 */

public class MyService extends Service implements TcpClientToSeiviceMessageListener {
    //    private ExecutorService executorService = Executors.newFixedThreadPool(10);
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        ToastUtil.showShortToast(this, "开启服务器");
        TcpService.getInstance().StartTcpService(this);
        UdpService.getInstance().StartUdpService(new UdpClientToSeiviceMessageListener() {
            @SuppressLint("NewApi")
            @Override
            public void clientToServiceListener(ChannelHandlerContext ctx, DatagramPacket datagramPacket) {
                String body = datagramPacket.content().toString(CharsetUtil.UTF_8);
                Log.e("收到客户端消息:", "收到客户端消息:" + body + "消息来源ip:" + datagramPacket.sender().getHostString() + ":" + datagramPacket.sender().getPort());
                ToastUtil.showShortToast(MyService.this, "收到客户端消息:" + body + "消息来源ip:" + datagramPacket.sender().getHostString() + ":" + datagramPacket.sender().getPort());
                Intent intent = new Intent();
                intent.setAction("action.SCREEN_ON");
                intent.putExtra("msg", "收到客户端消息:" + body + "消息来源ip:" + datagramPacket.sender().getHostString() + ":" + datagramPacket.sender().getPort());
                sendBroadcast(intent);

                DatagramPacket data = new DatagramPacket(Unpooled.copiedBuffer("来自服务端的消息,ip:" + CommonUtils.getLocalIpAddress(), CharsetUtil.UTF_8), datagramPacket.sender());
                ctx.writeAndFlush(data);
            }
        });
    }

    @Override
    public void tcpClientToSeiviceMessage(ChannelHandlerContext ctx, Object msg) {
        // 用于获取客户端发来的数据信息
        TcpMsgBean tcpMsgBean = (TcpMsgBean) msg;
        LogUtil.e("服务端收到客户端消息", tcpMsgBean.toString() + "==");
        Intent intent = new Intent();
        intent.setAction("action.SCREEN_ON");
        intent.putExtra("msg", tcpMsgBean.getBody());
        sendBroadcast(intent);

        TcpMsgBean msgBean = new TcpMsgBean();
        msgBean.setType("22222");
        String message = "收到来自服务端的消息,局域网ip:" + CommonUtils.getLocalIpAddress() + "==" + ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostName();
        msgBean.setBody(message);
        msgBean.setLength(message.length());
        NettyConfig.service_group.writeAndFlush(msgBean);
        ReferenceCountUtil.release(msg);
    }
}
